package in.muton.edushare.poll.panels;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.PollsDAO;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class PollList extends EdusharePanel {

	private static Log log = LogFactory.getLog(PollList.class);

	public PollList(String id, Universities univ, EduShareBasePage page) {
		super(id);
		this.entityId = univ.getUniversityId();
		this.entityType = Poll.POLL_UNIV;
		List<Poll> pList = new PollsDAO(getORMInterface()).getPollsByCreator(
				entityType, entityId);
		Collections.sort(pList);
		Collections.reverse(pList);
		pollList = new CayenneList<Poll>(pList);
		init();
	}

	public PollList(String id, Groups group, EduShareBasePage page) {
		super(id);
		this.entityId = group.getGroupId();
		this.entityType = Poll.POLL_GROUP;
		List<Poll> pList = new PollsDAO(getORMInterface()).getPollsByCreator(
				entityType, entityId);
		Collections.sort(pList);
		Collections.reverse(pList);
		pollList = new CayenneList<Poll>(pList);
		init();
	}

	public PollList(String id, CourseInstances course, EduShareBasePage page) {
		super(id);
		this.entityId = course.getCourseInstanceId();
		this.entityType = Poll.POLL_COURSE;
		List<Poll> pList = new PollsDAO(getORMInterface()).getPollsByCreator(
				entityType, entityId);
		Collections.sort(pList);
		Collections.reverse(pList);
		pollList = new CayenneList<Poll>(pList);
		init();
	}

	public PollList(String id, Departments dept, EduShareBasePage page) {
		super(id);
		this.entityId = dept.getDepartmentId();
		this.entityType = Poll.POLL_DEPT;
		List<Poll> pList = new PollsDAO(getORMInterface()).getPollsByCreator(
				entityType, entityId);
		Collections.sort(pList);
		Collections.reverse(pList);
		pollList = new CayenneList<Poll>(pList);
		init();
	}

	public PollList(String id, Users user, EduShareBasePage page) {
		super(id);
		List<Poll> tempCreatorPollList = new PollsDAO(getORMInterface())
				.getPollsByCreator(getLoggedInUser());

		List<Poll> tempUserPollList = new PollsDAO(getORMInterface())
				.getPollsByUser(getLoggedInUser());

		for (Poll p : tempUserPollList) {
			if (!tempCreatorPollList.contains(p))
				tempCreatorPollList.add(p);
		}
		Collections.sort(tempCreatorPollList);
		Collections.reverse(tempCreatorPollList);
		pollList = new CayenneList<Poll>(tempCreatorPollList);
		this.entityId = user.getUserId();
		this.entityType = Poll.POLL_INDI;
		init();
	}

	private void init() {
		add(CSSPackageResource
				.getHeaderContribution(PollList.class, "Poll.css"));
		add(new ListView<IModel<Poll>>("pollList", pollList) {
			@Override
			protected void populateItem(ListItem<IModel<Poll>> arg0) {
				arg0.add(new PollListItem("pollListItem", arg0
								.getModelObject()));
			}

			private static final long serialVersionUID = -6528140893176796628L;
		});
	}

	private CayenneList<Poll> pollList = null;
	private int entityType = 0;
	private int entityId = 0;
	private static final long serialVersionUID = 4162880126335631311L;
}
